package com.monco.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.monco.common.response.ApiResult;
import com.monco.entity.Rubbish;
import com.monco.service.IRubbishService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author com/monco
* @since 2020-03-09
*/
@Api(tags = "垃圾模块API")
@RestController
@RequestMapping("/rubbish")
public class RubbishController {

    @Resource
    private IRubbishService rubbishService;

    /**
     * 新增Rubbish方法
     *
     * @param rubbish
     * @return
     */
    @PostMapping
    public ApiResult save(@RequestBody Rubbish rubbish) {
        rubbishService.save(rubbish);
        return ApiResult.ok();
    }

    /**
     * 修改Rubbish方法
     *
     * @param rubbish
     * @return
     */
    @PutMapping
    public ApiResult update(@RequestBody Rubbish rubbish) {
        Rubbish rubbishUpdate = rubbishService.getById(rubbish.getId());
        rubbishUpdate.setRemarks(rubbish.getRemarks());
        rubbishService.updateById(rubbishUpdate);
        return ApiResult.ok();
    }

    /**
     * 查询列表
     *
     * @param rubbish
     * @return
     */
    @GetMapping("list")
    public ApiResult list(Rubbish rubbish) {
        return ApiResult.ok(rubbishService.getRubbishList(rubbish));
    }

    /**
     * 查询分页
     *
     * @param current 当前页
     * @param size    一页多少数据
     * @param rubbish
     * @return
     */
    @GetMapping("page")
    public ApiResult page(@RequestParam(required = false, defaultValue = "0") long current,
                          @RequestParam(required = false, defaultValue = "10") long size, Rubbish rubbish) {
        return ApiResult.ok(rubbishService.getRubbishPage(new Page<Rubbish>().setCurrent(current).setSize(size), rubbish));
    }

    /**
     * 删除方法
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public ApiResult delete(@RequestParam Long id) {
        rubbishService.removeById(id);
        return ApiResult.ok();
    }

    /**
     * 按照id获取单条记录
     *
     * @param id
     * @return
     */
    @GetMapping
    public ApiResult getOne(@RequestParam Long id) {
        return ApiResult.ok(rubbishService.getById(id));
    }
}
